package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type CounterpartyPermissionHandler interface {
	ListCounterpartyPermission() gin.HandlerFunc
	GetCounterpartyPermission() gin.HandlerFunc
	AddCounterpartyPermission() gin.HandlerFunc
	EditCounterpartyPermission() gin.HandlerFunc
	DeleteCounterpartyPermission() gin.HandlerFunc
	BatchDeleteCounterpartyPermission() gin.HandlerFunc
}

type counterpartyPermissionHandler struct {
	somsClient soms.Client
}

func (h *counterpartyPermissionHandler) ListCounterpartyPermission() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListCounterpartyPermissionDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListCounterpartyPermission(util.GetRPCContext(ctx), &api.ListCounterpartyPermissionRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.CounterpartyPermissionData{
				CounterpartyId: req.CounterpartyId,
	CounterpartyContactId: req.CounterpartyContactId,
	UserId: req.UserId,
	Username: req.Username,
	Nickname: req.Nickname,
	PermissionType: req.PermissionType,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *counterpartyPermissionHandler) GetCounterpartyPermission() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetCounterpartyPermission(util.GetRPCContext(ctx), &api.CounterpartyPermissionData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *counterpartyPermissionHandler) AddCounterpartyPermission() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.CounterpartyPermissionData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.CounterpartyPermissionData{
            Id:         data.Id,
            CounterpartyId: data.CounterpartyId,
	CounterpartyContactId: data.CounterpartyContactId,
	UserId: data.UserId,
	Username: data.Username,
	Nickname: data.Nickname,
	PermissionType: data.PermissionType,
	
        }
		err := h.somsClient.AddCounterpartyPermission(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *counterpartyPermissionHandler) EditCounterpartyPermission() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.CounterpartyPermissionData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.CounterpartyPermissionData{
            Id:         id,
            CounterpartyId: data.CounterpartyId,
	CounterpartyContactId: data.CounterpartyContactId,
	UserId: data.UserId,
	Username: data.Username,
	Nickname: data.Nickname,
	PermissionType: data.PermissionType,
	
        }
		if err = h.somsClient.EditCounterpartyPermission(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *counterpartyPermissionHandler) DeleteCounterpartyPermission() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteCounterpartyPermission(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *counterpartyPermissionHandler) BatchDeleteCounterpartyPermission() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteCounterpartyPermissionRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteCounterpartyPermission(util.GetRPCContext(ctx), &api.BatchDeleteCounterpartyPermissionRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

